package tgc.edu.fhh.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.fhh.custom.AjaxResult;
import tgc.edu.fhh.custom.BaseController;
import tgc.edu.fhh.entity.TbMenu;
import tgc.edu.fhh.service.SysRoleService;
import tgc.edu.fhh.service.TbMenuService;
import tgc.edu.fhh.web.form.TbMenuForm;

@Controller
@RequestMapping(value = "/menu")
public class TbMenuController extends BaseController<TbMenu, Integer, TbMenuForm>{
	@Autowired
	private TbMenuService service;
	@Autowired
	private SysRoleService roleService;
	
	@RequestMapping(value = "/combotree")
	@ResponseBody
	public Object combotree(Integer id) {
		Sort sort=Sort.by("idx1");
		Specification<TbMenu> spec = buildSpec1(null);
		List<TbMenu> list = service.findAll(spec, sort);
		return buildTree(list, id);
	}
	
	private List<HashMap<String, Object>> buildTree(List<TbMenu> list, Integer id) {
		List<HashMap<String, Object>> result = new ArrayList<>();
		for (TbMenu menu : list) {
			if (menu.getId() != id) {
				HashMap<String, Object> node = new HashMap<>();
				node.put("id", menu.getId());
				node.put("text", menu.getName());
				node.put("pid", menu.getParentId());
				List<HashMap<String, Object>> nodes = buildTree(menu.getChildren(), id);
				if(nodes.size()>0) {
					node.put("nodes", nodes);
				}
				result.add(node);
			}
		}
		return result;
	}

	private Specification<TbMenu> buildSpec1(Integer id) {
		Specification<TbMenu> specification = new Specification<TbMenu>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<TbMenu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if (id != null) {
					rules.add(cb.equal(root.get("parend").get("id"), id));
				}else {
					rules.add(cb.isNull(root.get("parent")));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}

	@Override
	public Object save(TbMenuForm form) {
		try {
			TbMenu model=new TbMenu();
			Integer id = form.getId();
			if(id!=null) {
				model=service.findById(id);
			}
			Integer parentId = form.getParentId();
			if(parentId!=null) {
				model.setParent(service.findById(parentId));
			}else {
				model.setParent(null);
			}
			Integer roleId = form.getRoleId();
			if(roleId!=null) { 
				model.setRole(roleService.findById(roleId)); 
			}else {
				model.setRole(null); 
			}
			 
			BeanUtils.copyProperties(form, model,"id","parent","role");
			service.save(model);
			return new AjaxResult("数据保存成功！");
			
		} catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}
	
}
